import axios from '@/utils/request'
import { GET_ARTICLE_LIST, GET_CHANNEL_LIST } from '../constants'
export function getChannelList() {
  return async (dispatch) => {
    const res = await axios({
      method: 'get',
      url: '/channels',
    })
    // console.log(res)
    dispatch({
      type: GET_CHANNEL_LIST,
      list: res.data.data.channels,
    })
  }
}
export function getArticleList(params) {
  return async (dispatch) => {
    const res = await axios({
      method: 'get',
      url: '/mp/articles',
      params,
    })
    // console.log(res)
    dispatch({
      type: GET_ARTICLE_LIST,
      articles: res.data.data,
    })
  }
}

export function del(id) {
  return async (dispatch) => {
    await axios({
      method: 'DELETE',
      url: `/mp/articles/${id}`,
    })
  }
}
/**
 *  添加或存入草稿
 * @param {*} draft
 * @param {*} data
 * @returns
 */
export const addAritcle = (draft = false, data) => {
  return async (dispatch) => {
    await axios({
      method: 'post',
      url: '/mp/articles',
      data,
      params: {
        draft,
      },
    })
  }
}
/**
 *  根据id 获取文章详情
 * @param {*} id
 * @returns
 */
export const getArticleInfo = (id) => {
  return async (dispatch) => {
    const res = await axios({
      method: 'get',
      url: `/mp/articles/${id}`,
    })
    // console.log(res)
    return res.data.data
  }
}
/**
 * 编辑文章
 * @param {*} draft
 * @param {*} data
 * @returns
 */
export const editArticle = (draft = false, data) => {
  return async (dispatch) => {
    await axios({
      method: 'put',
      url: `/mp/articles/${data.id}`,
      data,
      params: {
        draft,
      },
    })
  }
}
